package com.lzw.controller.student;

import com.lzw.anno.Log;
import com.lzw.pojo.RepairRequest;
import com.lzw.service.student.RRS;
import com.lzw.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/RR")
public class RRC {

    @Autowired
    private RRS rrs;

    //查看
    @GetMapping("/me")
    public Map<String, Object> get(@RequestHeader("Authorization") String token){
        Map<String, Object> response = new HashMap<>();
        try {
            //解析令牌
            Claims claims = JwtUtils.parseJWT(token.replace("Bearer ",""));
            String student = (String) claims.get("name");

            //查询
            List<RepairRequest> repairRequests = rrs.get(student);

            response.put("success",true);
            response.put("repairRequests",repairRequests);
        } catch (Exception e){
            response.put("success",false);
            response.put("message","无效令牌");
        }
        return response;
    }

    //报修申请
    @Log
    @PostMapping("/add")
    public String add(@RequestBody RepairRequest repairRequest){
        rrs.add(repairRequest);
        return "申请成功";
    }
}
